package com.jws.battleship;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import android.content.Context;
import android.database.Cursor;

public class Friend extends Person 
{
	private Integer mWins;
	private Integer mLosses;	
	private Date mLastOnline;
	
	public Date getLastOnline() {
		return mLastOnline;
	}
	public void setLastOnline(Date lastOnline) {
		this.mLastOnline = lastOnline;
	}

	public Integer getWins() {
		return mWins;
	}
	
	public void setWins(Integer wins) {
		this.mWins = wins;
	}
	
	public Integer getLosses() {
		return mLosses;
	}
	
	public void setLosses(Integer losses) {
		this.mLosses = losses;
	}
	
	public static String friendListToString(ArrayList<Friend> friends)
	{
		StringBuilder sb = new StringBuilder();
		
		for (Friend friend: friends) 
		{			
			sb.append(friend.getUniqueID());
			sb.append(";");
		}
		
		sb.deleteCharAt(sb.length()-1);
		
		return sb.toString();
	}
	
	//TODO string er null!?
	public static ArrayList<Friend> stringToFriendList(String string, Context context)
	{
		ArrayList<Friend> list = new ArrayList<Friend>();
		String[] value = string.split(";");
		
		DBSavedGames db = new DBSavedGames(context);
		db.open();
    	
		Cursor result = db.getAllFriends();
    	
    	for (int i = 0; i < result.getCount(); i++) 
    	{
    		result.moveToPosition(i);
    	
    		for(int j = 0; j < value.length; j++)
    		{
    			if(result.getString(DBSavedGames.FRIEND_TABLE_ID).equals(value[j]))
    			{
		    		Friend myFriend = new Friend();
					
		    		myFriend.setUniqueID(result.getInt(DBSavedGames.FRIEND_TABLE_ID));
					myFriend.setName(result.getString(DBSavedGames.FRIEND_TABLE_NAME));
					myFriend.setRank(result.getInt(DBSavedGames.FRIEND_TABLE_RANK));
					
					list.add(myFriend);
    			}
    		}
		}
    	
    	db.close();
		
		return list;
	}
	
	public static String getNameReadableStringFromList(ArrayList<Friend> friends)
	{
		StringBuilder sb = new StringBuilder();
		
		if(friends.size() == 1)
		{
			sb.append(friends.get(0).getName());
		}
		
		return sb.toString();
	}
}
